﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using CNCSoft.EWinform.ERPApps;
using CNCSoft.EData;
using System.Data.SqlClient;

using Common;

namespace CNCSoft.ErpApps.TMS.Academic
{
    public partial class frmLyLichHocVien : Form
    {
        DataTable dt;

        public frmLyLichHocVien()
        {
            InitializeComponent();
        }

        private void frmLyLichHocVien_Load(object sender, EventArgs e)
        {
            txtMaNienHoc.Text = GlobalVariable.WSInfo.getAppProperty("MaNienHocDefault");

            //Danh muc trinh trang
            DataTable dt = new DataTable();
            SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_TINHTRANG");
            repositoryItemLookUpEdit_TinhTrang.DataSource = dt;

            //Danh muc Che do
            DataTable dtcd = new DataTable();
            SQLHelper.Fill(dtcd, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_CHEDO");
            repositoryItemLookUpEdit_CheDo.DataSource = dtcd;

            //Danh muc Trang Thai
            DataTable dttt = new DataTable();
            SQLHelper.Fill(dttt, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_TRANGTHAI");
            repositoryItemLookUpEdi_TrangThai.DataSource = dttt;

            //Danh muc Mon the duc the thao
            DataTable dmMontd = new DataTable();
            SQLHelper.Fill(dmMontd, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_MONTHEDUC");
            repositoryItemLookUpEdit_MonTheDuc.DataSource = dmMontd;
        }

        private void txtMaNienHoc_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (txtMaNienHoc.Text == "") return;
                DataTable dt = new DataTable();
                SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, "SELECT * FROM DM_LOP WHERE MaNienHoc = '" + txtMaNienHoc.Text + "' Order by MaKhoi, STT");

                cboLop.DataSource = dt;
                cboLop.DisplayMember = "TenLop";
                cboLop.ValueMember = "MaLop";
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }

        private void cboLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                GetData(cboLop.SelectedValue.ToString());
            }
            catch (Exception ex)
            { MessageBox.Show(ex.Message); }
        }


        private void GetData(string strMaLop)
        {
            if (strMaLop == "") return;
            try
            {
                string strSELECT = "select MaHocTap, HOCSINH_LOP.MaHocSinh , HoLot, Ten, GioiTinh, MaCheDo, MaTrangThai, MaTinhTrang, MaMonTheDuc " +
                                    "from HOCSINH_LOP " +
                                    "join HOCSINH on HOCSINH_LOP.MaHocSinh = HOCSINH.MaHocSinh " +
                                    "where MaLop = '" + strMaLop + "' " +
                                    "order by STT_SoCai";
                dt = new DataTable();
                SQLHelper.Fill(dt, GlobalVariable.WSInfo.ConnectionString, strSELECT);
                if (dt != null)
                    grid_LyLichHS.DataSource = dt;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection cnn = new SqlConnection(GlobalVariable.WSInfo.ConnectionString);

                string sqlUpdate;
                sqlUpdate = "UPDATE HOCSINH_LOP SET MaCheDo = @pMaCheDo, MaTrangThai=@pMaTrangThai, MaTinhTrang=@pMaTinhTrang, MaMonTheDuc = @pMaMonTheDuc " +
                                   " WHERE MaHocTap = @pMaHocTap";

                SqlParameter[] pUpdate = new SqlParameter[5];

                //pUpdate[0] = new SqlParameter("@pGioiTinh", SqlDbType.Bit, 1, "GioiTinh");
                pUpdate[0] = new SqlParameter("@pMaCheDo", SqlDbType.VarChar, 2, "MaCheDo");
                pUpdate[1] = new SqlParameter("@pMaTrangThai", SqlDbType.VarChar, 2, "MaTrangThai");
                pUpdate[2] = new SqlParameter("@pMaTinhTrang", SqlDbType.VarChar, 2, "MaTinhTrang");
                pUpdate[3] = new SqlParameter("@pMaMonTheDuc", SqlDbType.VarChar, 10, "MaMonTheDuc");

                pUpdate[4] = new SqlParameter("@pMaHocTap", SqlDbType.VarChar, 50, "MaHocTap");
                
                SqlCommand cmdUpdate = new SqlCommand(sqlUpdate, cnn);

                cmdUpdate.Parameters.AddRange(pUpdate);

                SqlDataAdapter da = new SqlDataAdapter();
                da.UpdateCommand = cmdUpdate;
                try
                {
                    da.Update(dt);
                    dt.AcceptChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

                //Load lai
                if (cboLop.SelectedIndex >=0 )
                    GetData(cboLop.SelectedValue.ToString());

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void cmdBoQua_Click(object sender, EventArgs e)
        {
            //Load lai
            GetData(cboLop.SelectedValue.ToString());
        }



    }
}
